<template>
  <div>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="输入关键字：">
        <el-input v-model="keyWord" placeholder="关键字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selConditionStall()">查询</el-button>
      </el-form-item>
    </el-form>
    <div style="width: 55%; margin-left: 630px">
      <el-button type="danger" plain @click="recoverStall()">批量恢复</el-button>
      <el-button type="danger" plain @click="delStall()">批量删除</el-button>
    </div>
    <el-table :data="stallList.list" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="车位ID">
              <span>{{ props.row.stall_id }}</span>
            </el-form-item>
            <el-form-item label="车位编号">
              <span>{{ props.row.stall_code }}</span>
            </el-form-item>
            <el-form-item label="车位大小">
              <span>{{ props.row.stall_size }}</span>
            </el-form-item>
            <el-form-item label="车位状态">
              <span>{{ props.row.stall_state }}</span>
            </el-form-item>
            <el-form-item label="车位价格">
              <span>{{ props.row.stall_price }}</span>
            </el-form-item>
            <el-form-item label="所属车库">
              <span>{{ props.row.management.name}}</span>
            </el-form-item>
            <el-form-item label="车位所有者">
              <span>{{ props.row.users.user_name}}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
       <el-table-column type="selection" width="55" prop="stall_id">
      </el-table-column>
      <el-table-column label="车位照片" align="center">
        <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.photo"
            >
            </el-image>
        </template> 
      </el-table-column>
      <el-table-column label="车位大小" prop="stall_size" sortable> </el-table-column>
      <el-table-column label="车位状态" prop="stall_state" sortable> </el-table-column>
      <el-table-column label="车位价格" prop="stall_price" sortable> </el-table-column>
       <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="recoverOneStall(scope.row.stall_id)"
            >恢复</el-button
          >

          <el-button
            size="mini"
            type="danger"
            @click="delOneStall(scope.row.stall_id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="stallList.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="stallList.total"
    >
    </el-pagination>
    
  </div>
</template>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
export default {
  data() {
    return {
        stallList:{},
        pageSize: 5,
        currentPage:1,
        ids:[],
        keyWord:''
    }
     
  },
  methods:{
      localData(){
          this.$axios.get("/selAllStall", {
          params: {
            currentPage: this.currentPage,
            pageSize: this.pageSize,
          },
        }).
          then(res=>{
              this.stallList=res.data.data;
          }).catch(error=>{

          })
      },
      selConditionStall(){
        this.$axios.get("/selConditionStall",{
          params:{
            keyWord:this.keyWord,
            currentPage: this.currentPage,
            pageSize: this.pageSize,
          }
        }).then(res=>{
          this.stallList=res.data.data;
        }).catch(error=>{})
      },
      delStall(){
          this.$axios.post("/delStall",this.ids).
          then(res=>{
              this.localData();
              alert(res.data.messages);
          }).catch(error=>{

          })
      },
      delOneStall(id){
          this.$axios.get("/delOneStall/"+id).
          then(res=>{
              this.localData();
              alert(res.data.messages);
          }).catch(error=>{

          })
      },
      recoverStall(){
          this.$axios.post("/recoverStall",this.ids).
          then(res=>{
              this.localData();
              alert(res.data.messages);
          }).catch(error=>{

          })
      },
      recoverOneStall(id){
          this.$axios.get("/recoverOneStall/"+id).
          then(res=>{
              this.localData();
              alert(res.data.messages);
          }).catch(error=>{

          })
      },
      handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      if(this.keyWord==''){
        this.localData();
      }else{
        this.selConditionStall();
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if(this.keyWord==''){
        this.localData();
      }else{
        this.selConditionStall();
      }
    },
      handleSelectionChange(val) {
      var ids = []; //存放选中的id
      for (let item of val) {
        ids.push(item.stall_id);
      }
      //将选中的所有项的id放到数据模型中
      this.ids = ids;

      console.log(ids);
      //this.multipleSelection = val;
    },
  },
  mounted(){
      this.localData();
  }
};
</script>